import json
import datetime
# import QUANTAXIS as QA
import pandas as pd
# from pytdx.hq import TdxHq_API



# codes = ['000002', '000100']
# frequence = '5min'
# old_data = QA.QA_fetch_stock_min(codes, QA.QA_util_get_last_day(QA.QA_util_get_real_date(str(datetime.date.today()))),
#                                                str(datetime.datetime.now()), format='pd',
#                                                frequence=frequence).set_index(['datetime', 'code'])
# print(old_data)
# data = pd.read_csv('test.csv')
# data = data.set_index(['datetime', 'code'])
# # print(data)
# # print(data.iloc[-1, :])
# # print(data.index.levels[0][-1])
# print(data.loc[(data.index.levels[0][-1], slice(None)), :])
# print(data.loc[(data.index.levels[0][-1], 2), :])

# api = TdxHq_API()
# with api.connect('119.147.212.81', 7709):
#     # data = api.get_security_bars(9, 0, '000001', 0, 10)  # 返回普通list
#     data = api.to_df(api.get_security_bars(8, 0, '002664', 0, 5))  # 返回DataFrame
#     print(data.head())

# df = pd.DataFrame({'A': [1, 2, 3],
#                    'B': [400, 500, 600]})
# new_df = pd.DataFrame({'A': [4, 5, 6, 7],
#                        'B': [7, 8, 9, 10]})
# # df.update(new_df)
# # df = df.append(new_df)
# df = pd.concat([df, new_df], join='outer')
# print(df)

# df = pd.read_csv('test1.csv', index_col=['datetime', 'code'])
# print(df.head())
# df_new = pd.read_csv('test.csv', index_col=['datetime', 'code'])
# df.update(df_new)
# # df = df.append(df_new)
# # df = pd.concat([df, df_new], join='inner')
# df.reset_index(inplace=True)
# df_new.reset_index(inplace=True)
# df = df.merge(df_new, how='outer')
# df.set_index(['datetime', 'code'], inplace=True)
# print(df)

# def MACD_JCSC(dataframe, SHORT=12, LONG=26, M=9):
#     """
#     1.DIF向上突破DEA，买入信号参考。
#     2.DIF向下跌破DEA，卖出信号参考。
#     """
#     CLOSE = dataframe.close
#     DIFF = QA.EMA(CLOSE, SHORT) - QA.EMA(CLOSE, LONG)
#     DEA = QA.EMA(DIFF, M)
#     MACD = 2*(DIFF-DEA)
#
#     CROSS_JC = QA.CROSS(DIFF, DEA)
#     CROSS_SC = QA.CROSS(DEA, DIFF)
#     return pd.DataFrame({'DIFF': DIFF, 'DEA': DEA, 'MACD': MACD, 'CROSS_JC': CROSS_JC, 'CROSS_SC': CROSS_SC, 'CLOSE': CLOSE})
#
# df = pd.read_csv('market.csv', index_col=['datetime', 'code'])
# print(df.head())
# df = df.groupby(level=1, sort=False).apply(MACD_JCSC)
# print(df.tail(20))

# list = [0]
# list = [x for x in range(10)]
# # list.append(1e5)
# list.insert(0, 0)
# list.append(1e5)
# print(list)

import numpy as np
from QUANTAXIS.QAIndicator.base import SUMBARS
# df = pd.DataFrame(np.random.randn(2, 2), columns=['A', 'B'])
df = pd.Series([1, 0, 1, 0, 1], index=np.arange(0, 5))
print(SUMBARS(df, A=2))
print(df.shift(1))
